﻿using JHT.EFMigrationTest.ClassLibrary1.Entity;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure.Interception;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Z.EntityFramework.Plus;

namespace JHT.EFMigrationTest.ClassLibrary1
{
    public class TestDbContext : DbContext
    {
        public TestDbContext() : base("ABPCommon")
        {
            QueryFilterManager.Filter<Books>(p => p.Where(t => !string.IsNullOrEmpty(t.BookAuthor)));// 添加全局过滤程序
        }
        static TestDbContext()
        {
            //DbInterception.Add() 拦截接口
            Database.SetInitializer<TestDbContext>(null);// 设置数据库初始化策略
            //Database.SetInitializer(new DropCreateDatabaseAlways<TestDbContext>());// 系统自定义初始化策略
            //Database.SetInitializer(new TestDbContextInitializer());// 自定义数据库初始化策略
        }

        public DbSet<Students> Students { get; set; }

        public DbSet<StudentsAddress> StudentsAddresses { get; set; }

        public DbSet<ClassInfo> ClassInfos { get; set; }

        public DbSet<TeacherInfo> TeacherInfos { get; set; }

        public DbSet<Books> Books { get; set; }

        public DbSet<StudentClass> StudentClasses { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.HasDefaultSchema("dbo");
            modelBuilder.Types().Configure(p =>
            {
                if (p.GetType() != typeof(StudentClass))
                {
                    p.MapToStoredProcedures();
                }
            });
            modelBuilder.ComplexType<Address>();// 设置复杂类
            modelBuilder.Configurations.Add(new StudentsMap());
            modelBuilder.Configurations.Add(new StudentsAddressMap());
            modelBuilder.Configurations.Add(new ClassInfoMap());
            modelBuilder.Configurations.Add(new TeacherInfoMap());
            modelBuilder.Configurations.Add(new BooksMap());
            modelBuilder.Configurations.Add(new StudentClassMap());
        }
    }
}
